package ar.edu.unicen.exa.isistan.archassis.view.web;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.StringWriter;
import java.util.List;
import java.util.Properties;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
import org.apache.velocity.runtime.resource.util.StringResourceRepository;

import ar.edu.unicen.exa.isistan.archassis.ia.api.ISummaryData;
import ar.edu.unicen.exa.isistan.archassis.utils.HTMLResource;
import ar.edu.unicen.exa.isistan.archassis.utils.JarResource;

public class SummaryExport {
	public static final String HEADER_TEMPLATE = "header";
	public static final String SUMMARY_TEMPLATE = "summary";
	public static final String DIFF_TEMPLATE = "uml-diff";
	public static final String CLASSESLIST_TEMPLATE = "classes-list";
	public static final String COMPLETECLASS_TEMPLATE = "completeClass";
	
	private static SummaryExport instance = null;
	public static SummaryExport getInstance() {
		if (instance == null) {
			instance = new SummaryExport();
		}
		return instance;
	}

	VelocityEngine ve = null;

	private SummaryExport() {
		try {
			ve = getVelocityEngine();
			handleTemplate(HEADER_TEMPLATE);
			handleTemplate(DIFF_TEMPLATE);
			handleTemplate(SUMMARY_TEMPLATE);
			handleTemplate(CLASSESLIST_TEMPLATE);
			handleTemplate(COMPLETECLASS_TEMPLATE);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void handleTemplate(String templateName)	{
		String myTemplateBody = null;
		StringResourceRepository repository = null;

		try	{
			// Reading Template Body from the template file(.vm file) in the jar
			myTemplateBody = JarResource.getInstance().getResourceString("veltemp/" + templateName + ".vm");

			// Setting the template body in string repository with a template
			// name. Here the template name is used as a key for future mapping.
			repository = StringResourceLoader.getRepository();
			repository.putStringResource(templateName, myTemplateBody);
			System.out.println("loaded '" + templateName + "' " + myTemplateBody.length());
		} catch (Exception e) {
			System.out.println("Oops! We have an exception");
			e.printStackTrace();
		}

	}

	public File onError() throws FileNotFoundException {
		String file = "onError.html";
		return printToTmpFile(file, "Error!!!");
	}
	
	public File execute(String templateName, VelocityContext context) throws ResourceNotFoundException, ParseErrorException, Exception {
		/*  next, get the Template  */
		Template t = ve.getTemplate( templateName );
		
		String file = templateName + "-temp.html";
		
		/* now render the template into a StringWriter */
		StringWriter writer = execute(templateName);
		t.merge( context, writer );
		/* show the World */
		//System.out.println( writer.toString() );
		return printToTmpFile(file, writer);
	}
	
	private StringWriter execute(String templateName) throws ResourceNotFoundException, ParseErrorException, Exception {
		Template t = ve.getTemplate( HEADER_TEMPLATE );
		
		VelocityContext context = new VelocityContext();
		context.put("templateName", templateName);
		StringWriter writer = new StringWriter();
		t.merge( context, writer );
		
		return writer;
	}
	
//	public File exportJavaClass(List<JavaClass> classesList) throws ResourceNotFoundException, ParseErrorException, Exception {
//
//		VelocityContext context = new VelocityContext();
//		context.put("resultados", classesList);
//		
//		return execute(CLASSESLIST_TEMPLATE, context);
//	}
	
	public File exportSummary(List<ISummaryData> lista) throws ResourceNotFoundException, ParseErrorException, Exception {

		VelocityContext context = new VelocityContext();
		context.put("resultados", lista);

		return execute(SUMMARY_TEMPLATE, context);
	}
	

	private static VelocityEngine getVelocityEngine() throws Exception	{
		// Initializes the velocity engine with properties. We should specify
		// the resource loader as string and the class for
		// string.resource.loader in properties
		Properties p = new Properties();

		p.setProperty("resource.loader", "string");
		p.setProperty("string.resource.loader.class",
			"org.apache.velocity.runtime.resource.loader.StringResourceLoader");
		VelocityEngine engine = new VelocityEngine();
		engine.init(p);

		return (engine);
	}
	
	public static File printToTmpFile(String fileName, StringWriter content) throws FileNotFoundException {

		return printToTmpFile(fileName, content.toString());
	}

	public static File printToTmpFile(String fileName, String content) throws FileNotFoundException {
		File file = new File(HTMLResource.getHTMLWorkspace(), fileName);
		FileOutputStream fout = new FileOutputStream(file);
		
		PrintStream ps = new PrintStream(fout);
		ps.println(content.toString());
		ps.println(JarResource.getInstance().getResourceString("veltemp/footer.html"));
		return file;
	}
	
	public static void main(String[] args) {
		
	}

}
